﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

public partial class Admin_Article_ArticleList : System.Web.UI.Page
{
    MatchIns.BLL.PageInfo pi = new MatchIns.BLL.PageInfo();
    List<MatchIns.Model.TArticleCategory> categorys = new MatchIns.BLL.TArticleCategory().GetList();
    protected void Page_Load(object sender, EventArgs e)
    {
        pi.PageSize = 12 ;
        pager.PageSize = pi.PageSize;
        if (!IsPostBack)
        {
            Delete();
            BindCategorys();
            BindData();
        }
    }

    protected void BindCategorys()
    {
        List<MatchIns.Model.TArticleCategory> ds = new List<MatchIns.Model.TArticleCategory>();
        new MatchIns.BLL.TArticleCategory().GetList(0, categorys, ds, "");

        category.Items.Clear();
        category.DataSource = ds;
        category.DataTextField = "CategoryName";
        category.DataValueField = "ArticleCategoryId";
        category.DataBind();
        category.Items.Insert(0, new ListItem("请选择...","0"));

    }
    protected void BindData()
    {

        MatchIns.BLL.TArticle bll = new MatchIns.BLL.TArticle();
        gv.DataSource = new MatchIns.BLL.TArticle().GetPageList(pi).List;
        gv.DataBind();
        pager.RecordCount = pi.RecordCount;
    }

    protected void Delete()
    {
        int articleId;
        if (int.TryParse(Request.QueryString["articleId"], out articleId))
            new MatchIns.BLL.TArticle().Delete(articleId);
    }

    protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        pi.PageIndex = e.NewPageIndex;
        BindData();
    }

    protected string GetCategoryName(object categoryID)
    {
        MatchIns.Model.TArticleCategory model =
            categorys.Find(delegate(MatchIns.Model.TArticleCategory d) { return d.ArticleCategoryId == Convert.ToInt32(categoryID); });

        if (model == null)
            return "";
        else
            return model.CategoryName;
    }
    protected void search_Click(object sender, EventArgs e)
    {
        pi.Where = " where 1=1";
        if (title.Text.Trim().Length > 0)
            pi.Where+=" and title like '%"+title.Text.Trim().Replace("'","''")+"%'";
        if (author.Text.Trim().Length > 0)
            pi.Where += " and author = '" + author.Text.Trim().Replace("'", "''") + "'";
        if (category.SelectedValue != "0")
            pi.Where += " and ArticleCategoryId="+category.SelectedValue;
        int _weight;
        if(int.TryParse(weight.Text,out _weight))
            pi.Where+=" and weight="+_weight.ToString();
        pi.PageIndex = 1;
        BindData();
    }
}
